﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Hotel_application.Models;

namespace Hotel_application.Controllers
{
    public class RegisterController : Controller
    {
        //
        // GET: /Register/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Registering()
        {      

            var ks = new KhachSanEntities();
            string max_maTK = ks.CongTies.Select(e => e.maTK).OrderByDescending(p => p).FirstOrDefault();
            max_maTK = max_maTK.Substring(2);
            int max_maTK_int = Convert.ToInt32(max_maTK);
            max_maTK_int += 1;
            max_maTK = Convert.ToString(max_maTK_int).PadLeft(6, '0');
            string maTK = "CT" + max_maTK;

            TaiKhoan tk = new TaiKhoan();
            tk.maTK = maTK;
            tk.passwordTK = Request["matKhau"];
            tk.email = Request["email"];
            ks.TaiKhoans.Add(tk);
            ks.SaveChanges();

            string max_maCT = ks.CongTies.Select(e => e.maCT).OrderByDescending(p => p).FirstOrDefault();
            max_maCT = max_maCT.Substring(2);
            int max_maCT_int = Convert.ToInt32(max_maCT);
            max_maCT_int += 1;
            max_maCT = Convert.ToString(max_maCT_int).PadLeft(6, '0');
            string maCT = "CT" + max_maCT;

            CongTy ct = new CongTy();
            ct.maCT = maCT;
            ct.maTK = maTK;
            ct.tenCT = Request["tenCT"];
            ct.tenTKNH = Request["tenTKNH"];
            ct.maTKNH = Request["maTKNH"];
            ct.diaChi = Request["diaChi"];
            ct.soDT = Request["soDT"];
            ks.CongTies.Add(ct);
            ks.SaveChanges();

            return View("sucess");
        }
    }
}
